//
//  ATM.m
//  RRQS
//
//  Created by kyjun on 16/4/23.
//
//

#import "ATM.h"

@interface ATM ()<UITextFieldDelegate>

@property(nonatomic,strong) UILabel* labelTitle;
@property(nonatomic,strong) UITextField* txtCard;
@property(nonatomic,strong) UITextField* txtMoney;


@property(nonatomic,strong) UIButton* btnConfirm;
@property(nonatomic,strong) UILabel* labelMark;

@end

@implementation ATM

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请提现";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self layoutUI];
    
}



#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    
    [self.view addSubview:self.labelTitle];
    [self.view addSubview:self.txtCard];
    [self.view addSubview:self.txtMoney];
    
    [self.view addSubview:self.btnConfirm];
    [self.view addSubview:self.labelMark];
    
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.txtCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelTitle.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.labelTitle);
    }];
    
    [self.txtMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.txtCard.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.txtCard);
    }];
    
    
    [self.btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.txtMoney.mas_bottom).offset(50);
        make.left.right.equalTo(self.labelTitle);
        make.height.equalTo(self.labelTitle);
    }];
    
    [self.labelMark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btnConfirm.mas_bottom);
        make.left.right.equalTo(self.labelTitle);
        make.height.equalTo(self.labelTitle);
    }];
    
    
    
    
}


#pragma mark =====================================================  <UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if(range.length == 0 && range.location == 0 && [string isEqualToString:@"0"] ){
        return NO;
    }else if([WMHelper isEmptyOrNULLOrnil:string]){
        return YES;
    }else if([self.Identity.userInfo.account integerValue]<[textField.text integerValue]){
        return NO;
    }else if ([string isEqualToString:@"."]){
        return NO;
    }
    return YES;
}

#pragma mark =====================================================  SEL
-(IBAction)btnConfirmTouch:(id)sender{
    if([self checkForm]){
        [self showHUD];
        AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
        NSDictionary* arg = @{@"ince":@"set_emp_deposit",@"emp_id":self.Identity.userInfo.userID,@"amount":self.txtMoney.text};
        [mgr POST:kHost parameters:arg progress:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
            [WMHelper outPutJsonString:responseObject];
            NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
            if(flag ==1){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotifciationReLoadUserInfo object:nil];
                [self hidHUD:[responseObject objectForKey:@"sucess"]];
                self.txtMoney.text = @"";                
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [self hidHUD:[responseObject objectForKey:@"fail"]];
            }
        } failure:^(NSURLSessionDataTask *_Nonnull operation, NSError * _Nonnull error) {
            [self hidHUD:@"网络异常"];
        }];
    }
}

#pragma mark =====================================================  private method
-(BOOL)checkForm{
    if([WMHelper isEmptyOrNULLOrnil:self.txtMoney.text]){
        [self alertHUD:@"请输入提现金额"];
        return NO;
    }else if([self.txtMoney.text integerValue]<50){
        [self alertHUD:@"至少提现50元"];
        return NO;
    }
    return YES;
}

#pragma mark =====================================================  property package


-(UILabel *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[UILabel alloc]init];
        _labelTitle.font = [UIFont systemFontOfSize:12.f];
        _labelTitle.textColor = [UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        _labelTitle.text = [NSString stringWithFormat:@"每次至少提现50元,当前可提现余额%@元",self.Identity.userInfo.account];
    }
    return _labelTitle;
}

-(UITextField *)txtCard{
    if(!_txtCard){
        _txtCard = [[UITextField alloc]init];
        _txtCard.backgroundColor = [UIColor whiteColor];
        _txtCard.borderStyle=UITextBorderStyleNone;
        _txtCard.layer.borderColor =[UIColor colorWithRed:231/255.f green:231/255.f blue:231/255.f alpha:1.0].CGColor;
        _txtCard.layer.borderWidth = 1.0f;
        _txtCard.layer.masksToBounds = YES;
        _txtCard.userInteractionEnabled = NO;
        _txtCard.font = [UIFont systemFontOfSize:14.f];
        _txtCard.textColor = [UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        NSMutableString *str = [[NSMutableString  alloc] initWithString:[NSString stringWithFormat:@"%@ %@",self.Identity.userInfo.bankName,self.Identity.userInfo.bankCard]];
        [str replaceCharactersInRange:NSMakeRange(self.Identity.userInfo.bankName.length+10, 4) withString:@"****"];
        _txtCard.text = str;
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(0, 0, 15, 50);
        label.font = [UIFont systemFontOfSize:14.f];
        label.textColor = [UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        label.textAlignment = NSTextAlignmentCenter;
        _txtCard.leftView = label;
        _txtCard.leftViewMode =UITextFieldViewModeAlways;
    }
    return _txtCard;
}

-(UITextField *)txtMoney{
    if(!_txtMoney){
        _txtMoney = [[UITextField alloc]init];
        _txtMoney.backgroundColor = [UIColor whiteColor];
        _txtMoney.borderStyle=UITextBorderStyleNone;
        _txtMoney.delegate = self;
        _txtMoney.font = [UIFont systemFontOfSize:14.f];
        _txtMoney.keyboardType = UIKeyboardTypeNumberPad;
        _txtMoney.placeholder =@"请输入提现金额";
        _txtMoney.textColor = [UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        UIView* leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 50.f)];
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(15, 0, 70, 50);
        label.font = [UIFont systemFontOfSize:14.f];
        label.textColor =[UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        label.text = @"提现金额";
        label.textAlignment = NSTextAlignmentLeft;
        [leftView addSubview:label];
        label = [[UILabel alloc]initWithFrame:CGRectMake(90-8, 15, 2, 20.f)];
        label.backgroundColor =[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0];
        [leftView addSubview:label];
        _txtMoney.leftView = leftView;
        _txtMoney.leftViewMode =UITextFieldViewModeAlways;
    }
    return _txtMoney;
}



-(UIButton *)btnConfirm{
    if(!_btnConfirm){
        _btnConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnConfirm.backgroundColor = [UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0];
        [_btnConfirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnConfirm setTitle:@"确认提现" forState:UIControlStateNormal];
        [_btnConfirm addTarget:self action:@selector(btnConfirmTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnConfirm.layer.masksToBounds = YES;
        _btnConfirm.layer.cornerRadius = 5.f;
        _btnConfirm.layer.borderColor = [UIColor colorWithRed:231/255.f green:231/255.f blue:231/255.f alpha:1.0].CGColor;
    }
    return _btnConfirm;
}

-(UILabel *)labelMark{
    if(!_labelMark){
        _labelMark = [[UILabel alloc]init];
        _labelMark.font = [UIFont systemFontOfSize:12.f];
        _labelMark.textColor = [UIColor colorWithRed:64/255.f green:64/255.f blue:64/255.f alpha:1.0];
        _labelMark.numberOfLines = 0;
        _labelMark.lineBreakMode = NSLineBreakByWordWrapping;
        NSString* str = @"1.余额不足无法取消订单，建议提现时保留部分余额\n2.根据银行情况1-3个工作日到账";
        NSMutableAttributedString* attributeStr =[[NSMutableAttributedString alloc]initWithString:str];
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
        _labelMark.attributedText =attributeStr;
    }
    return _labelMark;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
